package com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.globals;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.nulldev.util.VariableAPI.ArrayUtils;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSEnvironment;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSObject;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSVariable;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.global.BaseJSNativeFunction;

public class BaseJSArray extends BaseJSObject {

	private static final BaseJSNativeFunction isArray = new BaseJSNativeFunction("isArray", "a") {

		@Override
		public Object invoked(BaseJSEnvironment e, BaseJSNativeFunction f, final Object... variables) {
			if (!f.valueCache.containsKey("a"))
				return false;
			Object a = f.valueCache.get("a");
			if (a instanceof BaseJSVariable)
				a = ((BaseJSVariable) a).get();
			if (ArrayUtils.isArray(a))
				return true;
			if (a instanceof BaseJSVariable)
				return ((BaseJSVariable) a).isArray();
			return false;
		}

	};
	private static Map<String, BaseJSObject> OBJECTS = new ConcurrentHashMap<String, BaseJSObject>();
	static {
		if (OBJECTS.isEmpty()) {
			OBJECTS.put("isArray", isArray);
		}
	}

	public BaseJSArray() {
		super("Array", Collections.unmodifiableMap(OBJECTS));
	}

}
